class Group {
	constructor (group){
		// 小组源数据
		this.group = group
		// 保存所有小组的一维数组
		this.allGroupList = []

		this._addRequireAttr(this.group)
		this._formatGroup(this.group)

	}
	/**
	 * [addRequireAttr 给数据结构添加必要的参数 path 和 isSelect]
	 * @param {[Array]} group   [小组]
	 * @param {[Array]} pathArr [小组路径]
	 */
	_addRequireAttr (group, pathArr){
		group.forEach((o, i, a) => {
			group[i].path = pathArr 
                            ? pathArr.concat({name: group[i].name, id: group[i].id}) 
                            : [{name: '全部', id: -1}, {name: group[i].name, id: group[i].id}]
            group[i].isSelect = false
			if(o.children.length > 0){
				this._addRequireAttr(o.children, group[i].path)
			}
		})
	}

	/**
	 * [formatGroup 抽取所有小组及子小组为一维数组]
	 * @param  {[Array]} group [小组源数据]
	 */
	_formatGroup (group){
		group.forEach((o, i, a) => {
			this.allGroupList.push({
				id: o.id,
				name: o.name
			})
			if(o.children.length > 0){
				this._formatGroup(o.children)
			}
		})
	}

	/**
	 * [findGroup 根据小组名字查找小组]
	 * @param  {[String]} groupName [小组名字]
	 * @return {[Array]}           [符合条件的数组]
	 */
	findGroupByName (groupName){
		return this.allGroupList.filter((o, i, a) => {
			if(o.name.indexOf(groupName) > -1){
				return true
			}
		})
	}

	/**
	 * [findGroup 根据小组Id查找小组]
	 * @param  {[String]} groupName [小组名字]
	 * @return {[Array]}           [符合条件的数组]
	 */
	findGroupById (groupId){
		return this.allGroupList.filter((o, i, a) => {
			if(o.id == groupId){
				return true
			}
		})[0]
	}

	/**
	 * [findGroup 根据小组Id查找小组树]
	 * @param  {[String]} groupId [小组名字]
	 * @return {[Array]}           [符合条件的数组]
	 */
	findGroupTreeById (groupId){
		let groupTree
		let finder = (group) => {
        	group.some((o, i, a) => {
				if(o.id == groupId){
	        		groupTree = o
	        		return true
	        	}
	        	if(o.children.length > 0){
					finder(o.children)
				}
	        })
        }
        finder(this.group)
        return groupTree
	}

	/**
	 * [selectGroupById 选中/移除 一个小组，该方法会自动 选中/移除 子小组，并检查当前小组同级的小组是否全选，来动态的把父级 选中/移除]
	 * @param  {[Number]} groupId [小组id]
	 * @return {[type]}         [description]
	 */
	setGroupById (groupId, label){
        // 将该组的 isSelect 设为 true
        let setCurrentGroup = (group, groupId, label) => {
        	group.some((o, i, a) => {
				if(o.id == groupId){
	        		group[i].isSelect = label
	        		return true
	        	}
	        	if(o.children.length > 0){
					setCurrentGroup(o.children, groupId, label)
				}
	        })
        }
        setCurrentGroup(this.group, groupId, label)

        // 将该组的子小组的 isSelect 设为 true
        let childTree = this.findGroupTreeById(groupId)
        let setChildGroup = (group) => {
        	group.forEach((o, i, a) => {
        		group[i].isSelect = label
        		if(o.children.length > 0){
	        		setChildGroup(o.children)
	        	}
        	})
        }
        setChildGroup([childTree])

        // 判断同级别的小组的 isSelect 是否都为true , 递归 将父级设为 true，否则设为 false
        let checkerParent = (groupId) => {
        	if(groupId != 0){
	        	let parentTree = this.findGroupTreeById(groupId)
		        let isSelectParent = parentTree.children.every((o, i, a) => {
		        	return o.isSelect
		        })
		        if(isSelectParent){
		        	setCurrentGroup(this.group, parentTree.id, true)
		        }else{
		        	setCurrentGroup(this.group, parentTree.id, false)
		        }
		        checkerParent(parentTree.pid)
	        }
        }
        checkerParent(childTree.pid)
        
    }

    getSelectedGroup (){
    	let groups = []
    	let finder = (group) => {
    		group.forEach((o, i, a) => {
    			if(o.isSelect){
    				groups.push(o.id+'')
    			}
    			if(o.children.length > 0){
    				finder(o.children)
    			}
    		})
    	}
    	finder(this.group)
    	return groups
    }


}

export default Group